<?php

class OrderController extends Controller
{

    /*
     * 用户端
    * 发送工人请求
    *
    */
    public function actionAddRequest()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $worker_id = Util::getParamNotEmpty("worker_id");
            $square = Util::getParamNotEmpty("square");
            $start_day = Util::getParamNotEmpty("start_day");
            $end_day = Util::getParamNotEmpty("end_day");
            $mobile = Util::getParamNotEmpty("mobile");
            $address = Util::getParamNotEmpty("address");
            $contact_name = Util::getParamNotEmpty("contact_name");

            //用户验证
            $user = UcUser::model()->findByPK($user_id);
            Util::checkExistAndNotDelete($user);


            //工人验证
            $worker = Worker::model()->findByPK($worker_id);
            Util::checkExistAndNotDelete($worker);

            //工人认证

            if($worker->is_verify != MsgConst::WORKER_VERIFY_STATUS_VERIFIED)
                throw new Exception("工人未认证");


            //验证该用户最多能预约工人数量


            //验证该用户之前是否已经约了该工人


            //添加订单
            OrderService::addrequest($user,$worker,$square,$mobile,$address,$contact_name,$start_day,$end_day);






            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取列表
    *
    */
    public function actionGetWorkerOrderList()
    {
        try {
            //参数验证
            $status = Util::getParamNotEmpty("status");

            //检测
            $worker_id = SessionService::getUserIDBySessionid();


            //获取列表
            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"worker_id",$worker_id);//工人搜索

            $fianl_status = OrderService::getFullStatus($status);

            //单一状态

            if(OrderService::isSingleStatus($fianl_status))
                $searchsql = Util::getSerarchStrAdd($searchsql,"status",$fianl_status);//条件搜索
            else
                $searchsql = Util::getSerarchStrAddIn($searchsql,"status",$fianl_status);//条件搜索







            //获取count
            $totalcount = OrderList::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time  desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = OrderList::model()->findAll($searchsql);




            //返回数据
            Util::responseList($totalcount, $list);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 工人端
    * 拒绝接单
    *
    */
    public function actionDenyOrder()
    {

        try {
            //检测
            $worker_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");
            $reason = Util::getParamNotEmpty("reason");


            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->worker_id != $worker_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_WAITING_ACCEPTED)
                throw new Exception("该订单已被处理");



           //拒绝订单
            OrderService::denyOrder($order,$reason);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 工人端
   * 接单
   *
   */
    public function actionAcceptOrder()
    {

        try {
            //检测
            $worker_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");



            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->worker_id != $worker_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_WAITING_ACCEPTED)
                throw new Exception("该订单已被处理");



            //拒绝订单
            OrderService::acceptOrder($order);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
   * 用户端
  * 取消接单
  *
  */
    public function actionCancelOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");



            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_WAITING_ACCEPTED)
                throw new Exception("该订单已被处理");



            //拒绝订单
            OrderService::cancelOrder($order,"用户取消订单");


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
     * 用户端
   * 获取项目列表
   *
   */
    public function actionGetUserOrderList()
    {
        try {
            //参数验证
            $status = Util::getParamNotEmpty("status");

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //获取列表
            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"user_id",$user_id);//工人搜索

            $fianl_status = OrderService::getFullStatus($status);

            //单一状态

            if(OrderService::isSingleStatus($fianl_status))
                $searchsql = Util::getSerarchStrAdd($searchsql,"status",$fianl_status);//条件搜索
            else
                $searchsql = Util::getSerarchStrAddIn($searchsql,"status",$fianl_status);//条件搜索

            //获取count
            $totalcount = OrderList::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time  desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = OrderList::model()->findAll($searchsql);




            //返回数据
            Util::responseList($totalcount, $list);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*用户端
    * 获取正在进行的项目的任务列表
    *
    */
    public function actionGetOrderTaskListPaging()
    {

        try {
            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //订单信息
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //是否是本人请求

            if($order->user_id != $user_id)
                throw new Exception("此订单非本人操作");


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"order_id",$order_id);


            //获取count
            $totalcount = WorkerTasks ::model()->count($searchsql);


                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = WorkerTasks::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }



    /*
  * 用户端
 *  支付定金接单
 *
 */
    public function actionDownpayOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");



            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_ACCEPTED)
                throw new Exception("该订单已被处理");



            //支付订单
            OrderService::downpayOrder($order);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
  * 用户端
 *  确认定金接单
 *
 */
    public function actionConfirmOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");
            $start_day = Util::getParamNotEmpty("start_day");
            $end_day = Util::getParamNotEmpty("end_day");

            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_DOWNPAY)
                throw new Exception("该订单已被处理");



            //支付订单
            OrderService::confirmOrder($order,$start_day,$end_day);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
* 用户端
*  开始订单
*
*/
    public function actionstartOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");

            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_CONFIRMED)
                throw new Exception("该订单已被处理");



            //支付订单
            OrderService::startOrder($order);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
* 用户端
*  开始订单
*
*/
    public function actionfinishOrder()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");
            $rating_quality = Util::getParamNotEmpty("rating_quality");
            $rating_efficiency = Util::getParamNotEmpty("rating_efficiency");
            $rating_satisfaction = Util::getParamNotEmpty("rating_satisfaction");
            $rating_manners = Util::getParamNotEmpty("rating_manners");
            $comment = Util::getParamNotEmpty("comment");

            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->status != MsgConst::ORDER_STATUS_WORKING)
                throw new Exception("该订单已被处理");



            //支付订单
            OrderService::finishOrder($order,$rating_efficiency,$rating_quality,$rating_satisfaction,$rating_manners,$comment);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
* 用户端
*  支付工程款
*
*/
    public function actionpayProjectMoney()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");
            $index = Util::getParamNotEmpty("index");

            if((int)$index< 1 || (int)$index >3)
                throw new Exception("工程款期数不正确");


            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            if($order->status != MsgConst::ORDER_STATUS_WORKING)
                throw new Exception("该订单不在进行中，不能付款");

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->pay_status !=  ((int)$index-1))
                throw new Exception("支付状态错误");



            //支付订单
            OrderService::payProjectOrder($order,$index);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
* 用户端
*  申请互检
*
*/
    public function actionrequestCheck()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");




            //验证存在
            $order = OrderList::model()->findByPK($order_id);

            Util::checkExistAndNotDelete($order);

            if($order->status != MsgConst::ORDER_STATUS_WORKING)
                throw new Exception("该订单不在进行中，不能付款");

            //工人验证
            if($order->user_id != $user_id)
                throw new Exception("不能处理别人的订单");

            //检查状态

            if($order->pay_status != 3)
                throw new Exception("工程款未付清，暂时不能申请");

            if($order->check_request_flag == 1)
                throw new Exception("已申请互检");



            //支付订单
            OrderService::requestOrder($order);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }



}